package edu.henu.personnel.service.catalog;

import edu.henu.personnel.constant.ErrorConstant;
import edu.henu.personnel.dao.CatalogDao;
import edu.henu.personnel.entity.Catalog;
import edu.henu.personnel.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: PersonnelProject
 * @description: 二级分类实现类
 * @author: zhuyang
 * @create: 2019-05-05 15:05
 */

@Transactional
@Service
public class CatalogServiceImpl implements CatalogService {

    @Autowired
    CatalogDao catalogDao;

    @Override
    public List<String> getAllCmemo() {
        List<String> allCmemo = catalogDao.findAllCmemo();
        return allCmemo;
    }

    @Override
    public List<Catalog> getAllCatalogInfo() {
        List<Catalog> allCatalog = catalogDao.findAllCatalog();
        return allCatalog;
    }

    @Override
    public List<Catalog> getCatalogInfoByMemo(String cmemo) {
        if (StringUtils.isBlank(cmemo)) {
            throw BusinessException.withErrorCode(ErrorConstant.Auth.USERNAME_PASSWORD_IS_EMPTY);
        }
        List<Catalog> allCatalogInfoByMemo = catalogDao.findAllCatalogInfoByMemo(cmemo);
        return allCatalogInfoByMemo;
    }

    @Override
    public Catalog getCatalogInfoById(Integer cid) {
        if (null == cid)
            throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);

        return catalogDao.findCatalogById(cid);
    }

    @Override
    public Catalog getCatalogInfoByName(String cname) {
        if (StringUtils.isBlank(cname))
            throw BusinessException.withErrorCode(ErrorConstant.Auth.USERNAME_PASSWORD_IS_EMPTY);

        return catalogDao.findCatalogByName(cname);
    }

    @Override
    public int addCatalogInfo(Catalog catalog) {
        if (null == catalog)
            throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);

        int result = catalogDao.insertCatalog(catalog);
        return result;
    }

    @Override
    public int modifyCatalogInfo(Catalog catalog) {
        if (null == catalog)
            throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);

        int result = catalogDao.updateCatalog(catalog);
        return result;
    }

    @Override
    public int removeCatalogInfo(Integer cid) {
        if (cid == null)
            throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);

        int result = catalogDao.deleteCatalogById(cid);
        return result;
    }
}
